package main

import "fmt"

//8ms 61%,6.2MB 30%
func minimumLength(s string) int {
	sLen := len(s)
	if sLen == 1 {
		return 1
	}
	i, j := 0, sLen-1
	for s[i] == s[j] && i < j {
		for i++; i < j; i++ {
			if s[i] != s[i-1] {
				break
			}
		}
		for j--; j > i; j-- {
			if s[j] != s[j+1] {
				break
			}
		}
	}
	return len(s[i : j+1])
}

//精简:4ms 84%,6.2MB 100%,同样的代码多次运行用时和内存消耗会不一样
func minimumLength1(s string) int {
	i, j := 0, len(s)-1
	for s[i] == s[j] && i < j {
		for i++; i < j; i++ {
			if s[i] != s[i-1] {
				break
			}
		}
		for j--; j > i; j-- {
			if s[j] != s[j+1] {
				break
			}
		}
	}
	return j - i + 1
}

func main() {
	s := "ab"
	fmt.Println(minimumLength(s))
	fmt.Println(s[1:1])
	fmt.Println(minimumLength1(s))
}
